import http from '@/utils/axios'
import { SharingListParams, AddResourceParams } from '@/api/model/resourceModel'
import { BasicResponseModel, UploadFileParams } from './model/basicModel'
import { ContentTypeEnum } from '@/enums/httpEnum'

enum Api {
  List = '/statement/resources/list', // 根据当前登录的人查询资源文件列表
  DeleteResource = '/statement/resources/delete', // 删除资源
  DeleteResourceFile = '/statement/resources/deleteRerourceFile', // 删除资源文件
  Upload = '/statement/resources/upload', // 上传资源文件
  Add = '/statement/resources/add' // 添加资源
}

/**
 * 根据当前登录的人查询资源文件列表
 * @param params
 */
export function getSharingList(params: SharingListParams) {
  return http.request<BasicResponseModel>(
    {
      url: Api.List,
      method: 'GET',
      params
    },
    {
      isTransformRequestResult: false
    }
  )
}

/**
 * 删除资源
 * @param resourceId
 */
export function deleteResource(resourceId: string) {
  return http.request<BasicResponseModel>({
    url: Api.DeleteResource,
    method: 'GET',
    params: { resourceId }
  })
}

/**
 * 删除资源文件
 * @param fileId
 */
export function deleteResourceFile(fileId: string) {
  return http.request({
    url: Api.DeleteResourceFile,
    method: 'GET',
    params: { fileId }
  })
}

/**
 * 添加资源
 * @param params
 */
export function addResource(params: AddResourceParams) {
  return http.request<BasicResponseModel>({
    url: Api.Add,
    method: 'POST',
    params
  })
}

/**
 * @description:  资源文件上传，单文件上传
 */
export function uploadResourceFile(params: UploadFileParams) {
  const formData = new FormData()
  formData.append('files', params.file, params.filename)
  formData.append('resourceId', params.resourceId)

  return http.request<BasicResponseModel>({
    url: Api.Upload,
    method: 'POST',
    data: formData,
    headers: {
      'Content-type': ContentTypeEnum.FORM_DATA
    }
  })
}
